
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>FAQ: 使用Django &#8212; Django 3.2.6.dev 文档</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="FAQ：获取帮助" href="help.html" />
    <link rel="prev" title="FAQ：安装" href="install.html" />



 
<script src="../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../index.html">Django 3.2.6.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../index.html">Home</a>  |
        <a title="Table of contents" href="../contents.html">Table of contents</a>  |
        <a title="Global index" href="../genindex.html">Index</a>  |
        <a title="Module index" href="../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="install.html" title="FAQ：安装">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="help.html" title="FAQ：获取帮助">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="faq-usage">
            
  <div class="section" id="s-faq-using-django">
<span id="faq-using-django"></span><h1>FAQ: 使用Django<a class="headerlink" href="#faq-using-django" title="永久链接至标题">¶</a></h1>
<div class="section" id="s-why-do-i-get-an-error-about-importing-django-settings-module">
<span id="why-do-i-get-an-error-about-importing-django-settings-module"></span><h2>为什么我在导入 <span class="target" id="index-2"></span><a class="reference internal" href="../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 时收到错误提示？<a class="headerlink" href="#why-do-i-get-an-error-about-importing-django-settings-module" title="永久链接至标题">¶</a></h2>
<p>保证：</p>
<ul class="simple">
<li>环境变量 <span class="target" id="index-4"></span><a class="reference internal" href="../topics/settings.html#envvar-DJANGO_SETTINGS_MODULE"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code></a> 被设置成一个完全合格的 Python 模块（即 &quot;mysite.settings&quot;）。</li>
<li>所述模块在 <code class="docutils literal notranslate"><span class="pre">sys.path</span></code> (需引入 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">mysite.settings</span></code> 才会工作）</li>
<li>该模块不包含语法错误。</li>
</ul>
</div>
<div class="section" id="s-i-can-t-stand-your-template-language-do-i-have-to-use-it">
<span id="i-can-t-stand-your-template-language-do-i-have-to-use-it"></span><h2>我不喜欢 Django 的模板语言，能不能不使用它？<a class="headerlink" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it" title="永久链接至标题">¶</a></h2>
<p>我们认为我们的的模板引擎是最好的，但是我们意识到选择一个模板语言和信仰宗教相似。Django 没有什么必须的模板语言，如果你愿意使用 Jinja2、Mako 或其他的，请随意使用。</p>
</div>
<div class="section" id="s-do-i-have-to-use-your-model-database-layer">
<span id="do-i-have-to-use-your-model-database-layer"></span><h2>我是否必须使用你的模型/数据库层？<a class="headerlink" href="#do-i-have-to-use-your-model-database-layer" title="永久链接至标题">¶</a></h2>
<p>不。就像模板系统，模型/数据库层是与框架的其余部分分离的。</p>
<p>唯一例外的是：如果你使用其他的数据库库，你没办法使用 Django 自动生成的后台界面。这个应用是和 Django 数据库层耦合的。</p>
</div>
<div class="section" id="s-how-do-i-use-image-and-file-fields">
<span id="how-do-i-use-image-and-file-fields"></span><h2>我要怎么使用图片和文件字段？<a class="headerlink" href="#how-do-i-use-image-and-file-fields" title="永久链接至标题">¶</a></h2>
<p>在模型中使用 <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileField</span></code></a> 或 <a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImageField</span></code></a>  ，你还需要完成如下步骤：</p>
<ol class="arabic simple">
<li>在你的 setting 文件中，你需要定义：setting: <cite>MEDIA_ROOT</cite> 作为 Django 存储上传文件目录的完整路径。（为了提高性能，这些文件不会储存在数据库中）定义： setting: <cite>MEDIA_URL</cite> 作为该目录的基本公共 URL， 确保该目录能够被 Web 服务器的账户写入。</li>
<li>在你的模型中添加 <a class="reference internal" href="../ref/models/fields.html#django.db.models.FileField" title="django.db.models.FileField"><code class="xref py py-class docutils literal notranslate"><span class="pre">FileField</span></code></a> 或者 <a class="reference internal" href="../ref/models/fields.html#django.db.models.ImageField" title="django.db.models.ImageField"><code class="xref py py-class docutils literal notranslate"><span class="pre">ImageField</span></code></a>&nbsp;，可以通过定义:attr:<cite>~django.db.models.FileField.upload_to</cite>&nbsp;在 <a class="reference internal" href="../ref/settings.html#std:setting-MEDIA_ROOT"><code class="xref std std-setting docutils literal notranslate"><span class="pre">MEDIA_ROOT</span></code></a> 中明确一个子目录用来上传文件。</li>
<li>所有将被储存在数据库中的文件路径相同（相对于：setting: MEDIA_ROOT`）。你很想用由 Django 提供的：attr:<cite>~django.db.models.fields.files.FieldFile.url</cite>，比如， 如果：class:<cite>~django.db.models.ImageField</cite> 被叫做 <code class="docutils literal notranslate"><span class="pre">mug_shot</span></code>， 你就可以得到 <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">object.mug_shot.url</span> <span class="pre">}}</span></code> 图片模板的绝对路径。</li>
</ol>
</div>
<div class="section" id="s-how-do-i-make-a-variable-available-to-all-my-templates">
<span id="how-do-i-make-a-variable-available-to-all-my-templates"></span><h2>我如何让一个变量可用于我的所有模板之中？<a class="headerlink" href="#how-do-i-make-a-variable-available-to-all-my-templates" title="永久链接至标题">¶</a></h2>
<p>有时候你的模板会需要相同的东西。一个常见的例子是动态生成的菜单。乍一看，在模板上下文中添加一个通用字典似乎是合乎逻辑的。</p>
<p>最好方法是使用 <code class="docutils literal notranslate"><span class="pre">RequestContext</span></code>。关于如何使用它请查看：<a class="reference internal" href="../ref/templates/api.html#subclassing-context-requestcontext"><span class="std std-ref">使用 RequestContext</span></a>。</p>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">FAQ: 使用Django</a><ul>
<li><a class="reference internal" href="#why-do-i-get-an-error-about-importing-django-settings-module">为什么我在导入 <code class="docutils literal notranslate"><span class="pre">DJANGO_SETTINGS_MODULE</span></code> 时收到错误提示？</a></li>
<li><a class="reference internal" href="#i-can-t-stand-your-template-language-do-i-have-to-use-it">我不喜欢 Django 的模板语言，能不能不使用它？</a></li>
<li><a class="reference internal" href="#do-i-have-to-use-your-model-database-layer">我是否必须使用你的模型/数据库层？</a></li>
<li><a class="reference internal" href="#how-do-i-use-image-and-file-fields">我要怎么使用图片和文件字段？</a></li>
<li><a class="reference internal" href="#how-do-i-make-a-variable-available-to-all-my-templates">我如何让一个变量可用于我的所有模板之中？</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="install.html"
                        title="上一章">FAQ：安装</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="help.html"
                        title="下一章">FAQ：获取帮助</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/faq/usage.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">7月 23, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="install.html" title="FAQ：安装">previous</a>
     |
    <a href="index.html" title="Django FAQ" accesskey="U">up</a>
   |
    <a href="help.html" title="FAQ：获取帮助">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>